package com.hln.jingwushi.ui

import android.os.Bundle
import android.util.Log
import com.hln.jingwushi.MyBaseActivity
import com.hln.jingwushi.R
import com.hln.jingwushi.databinding.ActivityAlarmBinding
import com.hln.jingwushi.utils.DataCacheUtil
import com.gdsz.libbasic.utils.assist.getCurrentDataLong
import com.gdsz.libbasic.utils.assist.getWeek
import com.gdsz.libbasic.utils.assist.getYmd
import com.gdsz.libbasic.utils.assist.weekArray
import com.gdsz.libbasic.utils.view.throttleClick
import com.zgkxzx.modbus4And.requset.ModbusReq
import com.zgkxzx.modbus4And.requset.OnRequestBack

class AlarmSoundActivity : MyBaseActivity<ActivityAlarmBinding>() {
    var lightIsOpen = 0
    var soundIsOpen = 0
    override fun ActivityAlarmBinding.initBinding(savedInstanceState: Bundle?) {
        readLightData()
        readSoundData()
        val currentDate = getCurrentDataLong()
        binding.tvDate.text = getYmd(currentDate) + "   ${weekArray[getWeek(currentDate)]}"
        binding.tvName.text = DataCacheUtil.getName()
        binding.tvTip.text =
            "方舱运行状态正常，紧急联系电话：${DataCacheUtil.getContactTel()}，紧急联系人：${DataCacheUtil.getContactPerson()}"
    }

    override fun onClick() {
        super.onClick()
        binding.ivAlarmLightSwitch.throttleClick {
            if (lightIsOpen == 1) {
                lightClick(0)
            } else {
                lightClick(1)
            }
        }
        binding.ivAlarmSoundSwitch.throttleClick {
            if (soundIsOpen == 1) {
                soundClick(0)
            } else {
                soundClick(1)
            }
        }
    }


    fun lightClick(openState : Int) {
        ModbusReq.getInstance().writeRegisters(object : OnRequestBack<String?> {
            override fun onSuccess(t: String?) {
                lightIsOpen = openState
                updateLightOpenState()
            }

            override fun onFailed(msg: String) {
            }
        }, 1, 6, shortArrayOf(openState.toShort()))
    }

    fun soundClick(openState : Int) {
        ModbusReq.getInstance().writeRegisters(object : OnRequestBack<String?> {
            override fun onSuccess(t: String?) {
                soundIsOpen = openState
                updateSoundOpenState()
            }

            override fun onFailed(msg: String) {
            }
        }, 1, 7, shortArrayOf(openState.toShort()))
    }



    fun readLightData() {
        ModbusReq.getInstance().readInputRegisters(object : OnRequestBack<ShortArray> {
            override fun onSuccess(data: ShortArray) {
                runOnUiThread {
                    lightIsOpen = data[0].toInt()
                    updateLightOpenState()
                }
            }

            override fun onFailed(msg: String) {
                Log.e("asd", "readInputRegisters onFailed $msg")
            }
        }, 1, 6, 1)
    }

    fun readSoundData() {
        ModbusReq.getInstance().readInputRegisters(object : OnRequestBack<ShortArray> {
            override fun onSuccess(data: ShortArray) {
                runOnUiThread {
                    soundIsOpen = data[0].toInt()
                    updateSoundOpenState()
                }
            }

            override fun onFailed(msg: String) {
                Log.e("asd", "readInputRegisters onFailed $msg")
            }
        }, 1, 7, 1)
    }


    override fun notifyData(values: ShortArray) {
        runOnUiThread {
            binding.tvTemp.text = "温度 ${values[0]} ℃  湿度 ${values[1]} %"
        }
    }

    private fun updateLightOpenState() {
        if (lightIsOpen == 1) {
            binding.ivAlarmLightSwitch.setImageResource(R.mipmap.selector)
        } else {
            binding.ivAlarmLightSwitch.setImageResource(R.mipmap.unselector)
        }
    }

    private fun updateSoundOpenState() {
        if (soundIsOpen == 1) {
            binding.ivAlarmSoundSwitch.setImageResource(R.mipmap.selector)
        } else {
            binding.ivAlarmSoundSwitch.setImageResource(R.mipmap.unselector)
        }
    }
}